﻿@using ApiLib.RCL.Components
@using ApiLib.RCL.Components.AutoTable.Models
@using ApiLib.RCL.Components.AutoTable.Utils
@typeparam TItem

<style>
    .myform-label{
        line-height: 32px;
    }
    .table-container{
        td{
            vertical-align: middle;
        }
    }
    .bb-previewer.active .bb-viewer-canvas>img{
        max-width: 100%;
        max-height: 100%;
        width: auto;
        height: auto;
    }
</style>

<div class="row">
    <div class="col-10 row">
        @foreach (var (item, value) in _searchColumns)
        {
            <span class="col-auto myform-label">@item.Name</span>
            <div class="col-auto">
                <AutoTableFieldComponent Entity="Entity" ColumnAttribute="item"
                                         IsDisabled="false" IsSearch="true"></AutoTableFieldComponent>
            </div>
        }
        @if (_searchColumns.Count > 0)
        {
            <Button class="col-auto" OnClick="@(async _ => await ToSearch())" Text="查询"></Button>
        }
    </div>
    <div class="col-2 row justify-content-end">
        @if (HeaderButtonTemplate != null)
        {
            @HeaderButtonTemplate
        }
        else
        {
            @if (_tableAttribute.CanAdd)
            {
                <Button class="col-auto" Color="Color.Success" OnClick="ToAdd" Text="新增"></Button>
            }
            @if (_tableAttribute.CanImport)
            {
                <Button class="col-auto" Color="Color.Danger" OnClick="ToImportModel" Text="导入数据"></Button>
            }
            @if (_tableAttribute.CanExport)
            {
                <Button class="col-auto" Color="Color.Info" OnClick="ToExport" Text="导出数据"></Button>
            }
        }
    </div>
</div>

<div class="row">
    @*<Table @ref="_table"
           TItem="TItem"
           IsPagination="true"
           ShowLineNo="@_tableAttribute.ShowLineNo"
           ShowExtendEditButton="false"
           ShowExtendDeleteButton="false"
           ShowExtendButtons="true"
           PageItemsSource="@_tableAttribute.PageItemsSource"
           OnQueryAsync="OnQueryAsync">
        <TableColumns>
            @foreach (var (column,property) in _listColumns)
            {
                <TableColumn Field="@property.Name" FieldName="@property.Name" Text="@column.Name">
                    <Template Context="value">
                        $1$ <span>@value.Row.GetFieldValue(property.Name)</span> #1#
                        <span>@value.Value</span>
                    </Template>
                </TableColumn>
            }
        </TableColumns>
        <RowButtonTemplate>
            <LinkButton Text="编辑" OnClick="() => OpenEdit(context)"></LinkButton>
            <PopConfirmButton Placement="Placement.Bottom"
                              ConfirmButtonColor="Color.Danger"
                              Text="删除"
                              Content="确定删除吗？"
                              OnConfirm="() => ToDelete(context)"></PopConfirmButton>
        </RowButtonTemplate>
    </Table>*@
    <div class="table-container">
        <div class="table-shim table-scroll scroll">
            @if (_isLoading)
            {
                <SkeletonTable Columns="@_showColumnCount"/>
            }
            else
            {
                <table class="table table-striped table-hover">
                    <colgroup>
                        @if (_tableAttribute.ShowLineNo)
                        {
                            <col>
                        }
                        @foreach (var (column, property) in _listColumns)
                        {
                            <col style="width: @column.Width;">
                        }
                        <col>
                    </colgroup>
                    <thead>
                    <tr>
                        @if (_tableAttribute.ShowLineNo)
                        {
                            <th>
                                <div class="table-cell center">
                                    <span class="table-text">序号</span>
                                </div>
                            </th>
                        }
                        @foreach (var (column, property) in _listColumns)
                        {
                            <th>
                                <div class="table-cell center @column.TextAlignClass" style="width: @column.Width;">
                                    <span class="table-text">@column.Name</span>
                                </div>
                            </th>
                        }
                        <th>
                            <div class="table-cell center">
                                <span class="table-text">操作</span>
                            </div>
                        </th>
                    </tr>
                    </thead>
                    <tbody>
                    @for (var i = 1; i <= ListResponse.List.Count; i++)
                    {
                        var item = ListResponse.List[i - 1];

                        <tr>
                            @if (_tableAttribute.ShowLineNo)
                            {
                                <td>
                                    <div class="table-cell center">
                                        @i
                                    </div>
                                </td>
                            }
                            @foreach (var (column, property) in _listColumns)
                            {
                                <td>
                                    <div class="table-cell @column.TextAlignClass @column.TextWrapClass @column.TextEllipsisClass" style="width: @column.Width;">
                                        @{
                                            var value = column.GetFieldValue(item,property.Name)?.ToString() ?? "";
                                            if (column.FieldType is TableFieldType.Image or TableFieldType.File)
                                            {
                                                <FileViewComponent FilePaths="@value"></FileViewComponent>
                                            }
                                            else
                                            {
                                                @if (column.TextTooltip)
                                                {
                                                    <Tooltip IsHtml="true" Title="@value" Placement="Placement.Auto">
                                                        <span style="width: @column.Width;text-overflow: ellipsis">@value</span>
                                                    </Tooltip>
                                                }
                                                else
                                                {
                                                    <span>@value</span>
                                                }
                                            }
                                        }
                                    </div>
                                </td>
                            }
                            <td>
                                <div class="table-cell center">
                                    @if (RowButtonTemplate != null)
                                    {
                                        @RowButtonTemplate(item)
                                    }
                                    @if(ShowDefaultRowButtons)
                                    {
                                        @if (_tableAttribute.IsLinkButton)
                                        {
                                            <LinkButton class="col-auto" Text="查看"
                                                        IsOutline="@_tableAttribute.IsOutline"
                                                        Color="Color.Secondary"
                                                        IsAsync="true"
                                                        OnClick="@(async () => await ToDetail(item))">
                                            </LinkButton>
                                        }
                                        else
                                        {
                                            <Button class="col-auto" Text="查看" IsOutline="@_tableAttribute.IsOutline"
                                                    Color="Color.Secondary"
                                                    IsAsync="true"
                                                    OnClick="@(async () => await ToDetail(item))">
                                            </Button>
                                        }

                                        @if (_tableAttribute.CanEdit)
                                        {
                                            @if (_tableAttribute.IsLinkButton)
                                            {
                                                <LinkButton class="col-auto" Text="编辑"
                                                            IsOutline="@_tableAttribute.IsOutline" Color="Color.Danger"
                                                            IsAsync="true"
                                                            OnClick="@(async () => await ToEdit(item))">
                                                </LinkButton>
                                            }
                                            else
                                            {
                                                <Button class="col-auto" Text="编辑"
                                                        IsOutline="@_tableAttribute.IsOutline"
                                                        Color="Color.Danger"
                                                        IsAsync="true"
                                                        OnClick="@(async () => await ToEdit(item))">
                                                </Button>
                                            }
                                        }

                                        @if (_tableAttribute.CanDelete)
                                        {
                                            @if (_tableAttribute.IsLinkButton)
                                            {
                                                <LinkButton class="col-auto" Text="删除"
                                                            IsOutline="@_tableAttribute.IsOutline" Color="Color.Warning"
                                                            IsAsync="true"
                                                            OnClick="@(async () => await ToDelete(item))">
                                                </LinkButton>
                                            }
                                            else
                                            {
                                                <Button class="col-auto" Text="删除"
                                                        IsOutline="@_tableAttribute.IsOutline"
                                                        Color="Color.Warning"
                                                        IsAsync="true"
                                                        OnClick="@(async () => await ToDelete(item))">
                                                </Button>
                                            }
                                        }
                                    }
                                </div>
                            </td>
                        </tr>
                    }
                    </tbody>
                </table>
            }
        </div>
        <div class="nav nav-pages">
            <Pagination PageCount="@_pageCount" ShowPageInfo="true" ShowGotoNavigator="true"
                        PageIndex="@_pageIndex"
                        OnPageLinkClick="OnPageLinkClick">
                <PageInfoTemplate>
                    <div class="page-info me-2">@_pageInfoText</div>
                    <Select @bind-Value="_pageSize" Items="@_tableAttribute.GetPageSelectedItems()" OnValueChanged="OnPageItemsValueChanged"></Select>
                </PageInfoTemplate>
            </Pagination>
        </div>
    </div>
</div>
<Modal @ref="_importModal">
    <ModalDialog IsCentered="true" Title="导入" Size="Size.Medium" ShowCloseButton="false">
        <BodyTemplate>
            <div class="row">
                <div class="col-auto">
                    <Button style="width:100px;" Text="下载模板" OnClick="ToExportTemplate"></Button>
                </div>
                <div class="col-auto" style="margin:auto 0;">
                    <span>请根据模板导入文件。</span>
                </div>
            </div>
            <Divider></Divider>
            <div style="display:flex;justify-content:center;">
                <ButtonUpload style="padding:0;margin-right:10px;" BrowserButtonText="上传文件" TValue="string"
                              IsSingle="true" OnChange="@ToImport"></ButtonUpload>
                <Button style="width:100px;height:35px;margin-left:10px;" Text="返回" Color=Color.Secondary
                        OnClick="@(async () => await _importModal.Toggle())"></Button>
            </div>
        </BodyTemplate>
    </ModalDialog>
</Modal>